public class SolutionLCP29 {
    public int orchestraLayout(int num, int xPos, int yPos) {
        long layer=Math.min(xPos,yPos);
        layer=Math.min(layer,num-xPos-1);
        layer=Math.min(layer,num-yPos-1);
        long ans=(long)(num-1)*4*(layer%9);
        long temp=layer%9*(layer-1)*4%9;
        ans=(ans-temp+9)%9;
        if (xPos==layer){
            long p=yPos-layer;
            return (int)((ans+p)%9+1);
        }
        ans=(ans+num-2*layer-1)%9;
        if (yPos==num-layer-1){
            long p=xPos-layer;
            return (int)((ans+p)%9+1);
        }
        ans=(ans+num-2*layer-1)%9;
        if (xPos==num-layer-1){
            long p=num-layer-yPos-1;
            return (int)((ans+p)%9+1);
        }
        ans=(ans+num-2*layer-1)%9;
        long p=num-layer-xPos-1;
        return (int)((ans+p)%9+1);
    }

    public static void main(String[] args) {
        System.out.println(new SolutionLCP29().orchestraLayout(7,6,5));
    }
}
